﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using System.Runtime.InteropServices;

namespace Iso
{
    class VoxelTest : IScene
    {
        Color[][][] voxels;

        public virtual void Load()
        {
            // Camera
            if (Engine.camera != null)
            {
                Engine.camera.RemoveAnimators();
                Engine.camera.RemoveChildren();
                Engine.camera.Remove();
            }
            Engine.camera = Engine.device.SceneManager.AddCameraSceneNode();
            Engine.camera.SetProjectionMatrix(Matrix.Identity.BuildProjectionMatrixOrthoLH(90, 90, 0.01f, 1000000), true);
            Engine.camera.Position = new Vector3Df(0, 0, 0);
            Engine.camera.Target = new Vector3Df(0, 0, 1);

            // Generate Voxels
            voxels = new Color[10][][];
            for (int x = 0; x < 10; x++)
            {
                voxels[x] = new Color[10][];
                for (int y = 0; y < 10; y++)
                {
                    voxels[x][y] = new Color[10];
                    for (int z = 0; z < 10; z++)
                    {
                        if (Engine.device.Randomizer.GetFloat() > 0.5f)
                            voxels[x][y][z] = new Color((byte)Engine.device.Randomizer.GetInt()/* / Engine.device.Randomizer.MaxRandomInt * 255*/,
                                (byte)Engine.device.Randomizer.GetInt()/* / Engine.device.Randomizer.MaxRandomInt * 255*/,
                                (byte)Engine.device.Randomizer.GetInt()/* / Engine.device.Randomizer.MaxRandomInt * 255*/);
                        else
                            voxels[x][y][z] = new Color(0, 0, 0, 0);
                    }
                }
            }
        }

        public virtual void Unload()
        {

        }

        public virtual void Update()
        {

        }

        public virtual void Draw()
        {
            Engine.device.VideoDriver.BeginScene();

            int width = Engine.device.VideoDriver.ViewPort.Width, height = Engine.device.VideoDriver.ViewPort.Height;
            int[] data = new int[width * height];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < 50; z++)
                    {
                        if (x < voxels.Length && y < voxels[0].Length && z < voxels[0][0].Length)
                        {
                            if (voxels[x][y][z] != new Color(0, 0, 0, 0))
                            {
                                /*Vector2Di pos = Engine.device.SceneManager.SceneCollisionManager.GetScreenCoordinatesFrom3DPosition(
                                    new Vector3Df(x, y, z));
                                Engine.device.VideoDriver.DrawPixel(pos.X, pos.Y, voxels[x][y][z]);*/
                                data[y * width + x] = (int)voxels[x][y][z].ARGB;
                            } 
                        }
                    }
                }
            }
            /*for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    for (int z = 0; z < 10; z++)
                    {
                        if (voxels[x][y][z] != new Color(0, 0, 0, 0))
                        {
                            Vector2Di pos = Engine.device.SceneManager.SceneCollisionManager.GetScreenCoordinatesFrom3DPosition(
                                new Vector3Df(x, y, z));
                            Engine.device.VideoDriver.DrawPixel(pos.X, pos.Y, voxels[x][y][z]);
                        }
                    }
                }
            }*/
            Texture tex = Engine.device.VideoDriver.AddTexture(new Dimension2Di(width, height), "screenVoxel", ColorFormat.A8R8G8B8);
            if (tex.Painter.Lock(TextureLockMode.WriteOnly))
            {
                Marshal.Copy(data, 0, tex.Painter.MipMapLevelData, data.Length);
                tex.Painter.Unlock();
            }
            Engine.device.SceneManager.DrawAll();
            Engine.device.VideoDriver.EndScene();
        }
    }
}
